<?php
/**
 * 128位暗号化
 * AES128
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */
class AES128 {
    /**
     * 暗号化
     * encrypt
     * @param $data
     * @param $key
     */
    public function encrypt($data,$key) {
        $iv   = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
        $data = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data,  MCRYPT_MODE_ECB, $iv);
        $base64Data= base64_encode($data);
        return $base64Data;
    }
    
    /**
     * 暗号化された値を解読
     * decrypt
     * @param $data
     * @param $key
     */
    public function decrypt($data, $key) {
        if(""==$data) {
            return "";
        }
        $iv  = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
        $decryptData = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode( $data ),  MCRYPT_MODE_ECB, $iv);
        return  $decryptData;
    }
}